package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.ProfileGeneric;

import java.util.ArrayList;
import java.util.List;

public class ProfileGenericMapper {
    public static final String TABLE = "profile_generic";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+TABLE+"(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
            "device_code TEXT," +
            "channel_ln TEXT," +
            "entries_in_use integer," +
            "capture_period integer," +
            "profile_entries integer," +
            "source TEXT," +
            "name TEXT," +
            "username TEXT);";




    public static void delete(String device_code){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"'",null);
    }
    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }
    public static int count(){
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where username='" + HesAppStatic.getUsername() + "'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }

    public static void insert(ProfileGeneric item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("device_code", item.getDeviceCode());
        contentValues.put("channel_ln", item.getChannelLn());
        contentValues.put("entries_in_use", item.getEntriesInUse());
        contentValues.put("capture_period", item.getCapturePeriod());
        contentValues.put("profile_entries", item.getProfileEntries());
        contentValues.put("source", item.getSource());
        contentValues.put("name", item.getName());
        contentValues.put("username", HesAppStatic.getUsername());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }

    public static List<ProfileGeneric> queryByDeviceCode(String deviceCode){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "'");
        List<ProfileGeneric> resultList = getProfileGenerics(cursor);
        cursor.close();
        return resultList;
    }



    @NonNull
    private static List<ProfileGeneric> getProfileGenerics(Cursor cursor) {
        List<ProfileGeneric> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            ProfileGeneric item = new ProfileGeneric();
            item.setId(cursor.getLong(0));
            item.setDeviceCode(cursor.getString(1));
            item.setChannelLn(cursor.getString(2));
            item.setEntriesInUse(cursor.getInt(3));
            item.setCapturePeriod(cursor.getInt(4));
            item.setProfileEntries(cursor.getInt(5));
            item.setSource(cursor.getString(6));
            item.setName(cursor.getString(7));
            item.setUsername(cursor.getString(8));
            resultList.add(item);
        }
        return resultList;
    }




    public static List<ProfileGeneric> queryByUsername(){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "'");
        List<ProfileGeneric> resultList = getProfileGenerics(cursor);
        cursor.close();
        return resultList;
    }


    public static List<ProfileGeneric> queryByLimit(int pageNo, int pageSize){
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "' limit " + pageNo + "," + pageSize);
        List<ProfileGeneric> resultList = getProfileGenerics(cursor);
        cursor.close();
        return resultList;
    }
}
